package fr.emse.tatiana.commands;

import org.eclipse.jface.action.Action;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import fr.emse.tatiana.CorpusNavigationView;
import fr.emse.tatiana.replayable.CreatedReplayableInput;

/**
 * Creates a new editable replayable
 * 
 * @author dyke
 *
 */
public class CreateReplayableAction extends Action {

		private final IWorkbenchWindow window;
		private final String navigationID;
		private final String editorID;

		private static CreateReplayableAction instance;

		public static CreateReplayableAction getInstance() {
			return instance;
		}

		public CreateReplayableAction(IWorkbenchWindow window, String label, String edid, String navid) {
			this.window = window;
			this.navigationID = navid;
			this.editorID = edid;
			setText(label);
			// The id is used to refer to the action in a menu or toolbar
			setId(ICommandIds.CMD_NEW_CATEGORISATION_ANALYSIS);
			// Associate the action with a pre-defined command, to allow key bindings.
			//setActionDefinitionId(ICommandIds.CMD_NEW_CATEGORISATION_ANALYSIS);
			setImageDescriptor(fr.emse.tatiana.Activator.getImageDescriptor("/icons/sample2.gif"));
			this.setEnabled(false);
			instance = this;
		}


		@Override
		public void run() {
			if(window != null) {	
				IProject p = ((CorpusNavigationView) window.getActivePage().findView(this.navigationID)).getSelectedCorpus().getProject();
				IEditorInput input = new CreatedReplayableInput(p);
				try {
					window.getActivePage().openEditor(input, editorID);
				} catch (PartInitException e) {
					new MessageDialog(window.getShell(), 
							"Problem creating new replayable",
							null,
							"No idea why there was a problem",
							MessageDialog.ERROR,
							new String[]{"Ok"},
							0).open();
				}
			}
		}
	
	
}
